package com.dream.keigezhushou.Activity.acty.listen.local;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.acty.listen.myview.AlertDialog;
import com.dream.keigezhushou.Activity.adapter.LocalMusicAdapter;
import com.dream.keigezhushou.Activity.pop.MoreWindow;
import com.dream.keigezhushou.Activity.pop.MoreWindowToMusicMessage;
import com.dream.keigezhushou.Activity.view.ItemRemoveRecylerView01;
import com.dream.keigezhushou.Activity.view.OnItemClickListener;
import com.dream.keigezhushou.Activity.wigdt.DividerItemDeoration;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LocalMusicActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_scan)
    TextView tvScan;
    @BindView(R.id.rv_local)
    ItemRemoveRecylerView01 rvLocal;

    private ArrayList<String> local;


    private LocalMusicAdapter localMusicAdapter;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_music);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    //初始化界面
    protected void initView() {

        local = new ArrayList<>();
        for (int i = 'a'; i < 'l'; i++) {
            local.add("" + (char) i);
        }

        localMusicAdapter = new LocalMusicAdapter(this, local);
        rvLocal.setLayoutManager(new LinearLayoutManager(this));
        rvLocal.setAdapter(localMusicAdapter);

        rvLocal.addItemDecoration(new DividerItemDeoration(this, DividerItemDeoration.VERTICAL_LIST));
        localMusicAdapter.addHeaderView(View.inflate(this, R.layout.golde30s_collecte_headerview, null));
        localMusicAdapter.addFootView(View.inflate(this, R.layout.golde30s_collecte_footview, null));

        localMusicAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(LocalMusicActivity.this, "item播放", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDeleteClick(int mPosition) {

            }

            @Override
            public void onItemMoreClick(int position) {

                final MoreWindow moreWindow = new MoreWindow(View.inflate(LocalMusicActivity.this, R.layout.golde30s_pupwindow, null), LocalMusicActivity.this, RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
                moreWindow.showPopAtLocation(rvLocal, Gravity.BOTTOM);
                moreWindow.setItemListener(new MoreWindow.ItemClickListener() {
                    @Override
                    public void onClick() {
//                        Toast.makeText(HotRecommendActivity.this, "好样", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onSingDetail() {


                        WindowManager wm = (WindowManager) LocalMusicActivity.this
                                .getSystemService(Context.WINDOW_SERVICE);

                        int width = wm.getDefaultDisplay().getWidth();
                        int height = wm.getDefaultDisplay().getHeight();


                        MoreWindowToMusicMessage moreWindowToMusicMessage = new MoreWindowToMusicMessage(View.inflate(LocalMusicActivity.this, R.layout.pop_musicmessage, null), LocalMusicActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
//                       moreWindow.setBackColorState(0);
                        moreWindowToMusicMessage.showPopAtLocation(rvLocal, Gravity.BOTTOM);
                        moreWindow.backGroundAlpha(0.4f);


                    }
                });

            }
        });


//        localMusicAdapter.setOnItemClickListener(new MyItemClickListener() {
//            @Override
//            public void onItemClick(int position) {
//
//                Toast.makeText(LocalMusicActivity.this, "播放", Toast.LENGTH_SHORT).show();
//
//            }
//
//            @Override
//            public void onBtnClick(int position) {
//                final MoreWindow moreWindow = new MoreWindow(View.inflate(LocalMusicActivity.this, R.layout.pop_local, null), LocalMusicActivity.this, RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
//                moreWindow.showPopAtLocation(rvLocal, Gravity.BOTTOM);
//                moreWindow.setItemListener(new MoreWindow.ItemClickListener() {
//                    @Override
//                    public void onClick() {
//                        Toast.makeText(LocalMusicActivity.this, "popwindow-title", Toast.LENGTH_SHORT).show();
//
//                    }
//
//                    @Override
//                    public void onSingDetail() {
//
//
//                        WindowManager wm = (WindowManager) LocalMusicActivity.this
//                                .getSystemService(Context.WINDOW_SERVICE);
//
//                        int width = wm.getDefaultDisplay().getWidth();
//                        int height = wm.getDefaultDisplay().getHeight();
//
//
//                        MoreWindowToMusicMessage moreWindowToMusicMessage = new MoreWindowToMusicMessage(View.inflate(LocalMusicActivity.this, R.layout.pop_musicmessage, null), LocalMusicActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
////                       moreWindow.setBackColorState(0);
//                        moreWindowToMusicMessage.showPopAtLocation(rvLocal, Gravity.BOTTOM);
//                        moreWindow.backGroundAlpha(0.4f);
//
//
//                    }
//                });
//            }
//        });
//        rvLocal.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
////                Toast.makeText(LocalMusicActivity.this, "** " + local.get(position) + " **", Toast.LENGTH_SHORT).show();
//
//            }
//
//            @Override
//            public void onDeleteClick(int mPosition) {
//                if (mPosition > 0)
//                    localMusicAdapter.removeItem(mPosition - 1);
//
//            }
//
//            @Override
//            public void onItemMoreClick(int position) {
//
//            }
//        });
        rvLocal.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(LocalMusicActivity.this, "** " + local.get(position - 1) + " **", Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onDeleteClick(final int mPosition) {
                if (mPosition > 0) {
                    DisplayMetrics metrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    int width = (int) (metrics.widthPixels * 0.6);
                    AlertDialog myalerdialog = new AlertDialog(View.inflate(LocalMusicActivity.this, R.layout.alertdialog, null), LocalMusicActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
                    myalerdialog.showPopAtLocation(rvLocal, Gravity.CENTER_HORIZONTAL);

                    myalerdialog.setItemListener(new AlertDialog.ItemClickListener() {
                        @Override
                        public void onClick() {
                            localMusicAdapter.removeItem(mPosition - 1);
                        }
                    });

                }


            }

            @Override
            public void onItemMoreClick(int position) {

            }

        });
    }


    //初始化数据
    protected void initData() {


        ivReturn.setOnClickListener(this);
        tvScan.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.iv_return:

                //返回
                finish();
                break;
            case R.id.tv_scan:
                intent = new Intent(LocalMusicActivity.this, SearchMusicActivity.class);
                startActivity(intent);
                break;
        }

    }


    public interface MyItemClickListener {
        public void onItemClick(int position);

        public void onBtnClick(int position);
    }
}
